#include "remiitem.h"

RemiItem::RemiItem(int id)
{
    db = QSqlDatabase::database();
    this->id = id;
    highlight = false;

    QSqlQuery q("SELECT `id`, `titel`, `gtin`, `addon`, `menge`, `datetime`, `paket_id`, `preis` FROM `remission` WHERE `id` = '" + QVariant(id).toString() + "';", db);
    if ( q.first() )
    {
        this->title =       q.value(1).toString();
        this->gtin =        q.value(2).toString();
        this->addon =       q.value(3).toString();
        this->quantity =    q.value(4).toInt();
        this->lastedit =    q.value(5).toDateTime();
        this->paket =       q.value(6).toInt();
        this->preis =       q.value(7).toFloat();
        this->objektnr =    da.getObjektNr(this->gtin, this->addon);

        this->setFlags(Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsDragEnabled|Qt::ItemIsEditable);
        this->setData(0, Qt::UserRole, this->id);
    }
    q.finish();
}

RemiItem::RemiItem(QString gtin, QString addon, int quantity)
{
    db = QSqlDatabase::database();
    highlight = false;

    this->title =       QString();
    this->gtin =        gtin;
    this->addon =       addon;
    this->quantity =    quantity;
    this->lastedit =    QDateTime::currentDateTime();
    this->paket =       -1;
    this->preis =       0.0;
    this->objektnr =    da.getObjektNr(this->gtin, this->addon);

    QSqlQuery stammdaten("SELECT `titel`, `preis` FROM `stammdaten` WHERE `objektnr` = '" + objektnr + "';", db);
    if ( stammdaten.first() )
    {
        title = stammdaten.value(0).toString();
        if ( da.isPresseGTIN(gtin) )
            preis = QVariant(gtin.mid(8, 4)).toFloat() / 100;
        else
            preis = stammdaten.value(1).toFloat();

        QSqlQuery q(db);
        q.prepare("INSERT INTO `remission` ( `titel`, `gtin`, `addon`, `menge`, `preis`, `datetime`, `archiv_id`, `paket_id` ) VALUES ( :titel, '" + gtin + "', '" + addon + "', '" + QVariant(this->quantity).toString() + "', :preis, '" + lastedit.toString("yyyy-MM-dd hh:mm:ss.zzz") + "', '-1', '-1' );");
        q.bindValue(":titel", title);
        q.bindValue(":preis", preis);
        q.exec();
        this->id = q.lastInsertId().toInt();
    }
    stammdaten.finish();

    this->setFlags(Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsDragEnabled|Qt::ItemIsEditable);
    this->setData(0, Qt::UserRole, this->id);
}

void RemiItem::setData(int column, int role, const QVariant &value)
{
    if ( role == Qt::EditRole )
    {
        /** Titel ändern **/
        if ( column == 0 )
        {
            if ( !value.toString().isEmpty() )
            {
                if ( value.toString() != title )
                {
                    setTitle( value.toString() );
                    emitDataChanged();
                }
            }
            else
                QMessageBox::warning(0, tr("Titel ändern"), tr("Bitte geben Sie einen gültigen Titel ein."));
        }

        /** Menge ändern **/
        if ( column == 3 )
        {
            if ( value.toInt() != quantity )
                setQuantity(value.toInt());
            if ( quantity == 0 )
                return;
        }
    }

    QTreeWidgetItem::setData(column, role, value);
}

QVariant RemiItem::data(int column, int role) const
{
    switch ( role )
    {
    case Qt::DisplayRole:
    case Qt::EditRole:
        switch ( column )
        {
        case 0:
            return title;
        case 1:
            return da.getAusgabe(gtin, addon);
        case 2:
            return QString("%L1 €").arg(preis,0,'f',2);
        case 3:
            return QVariant(quantity).toString();
        case 4:
            return QString("%L1 €").arg(quantity * preis,0,'f',2);
        case 5:
            return lastedit.toString("dd.MM.yy hh:mm");
        case 6:
            return gtin;
        case 7:
            return addon;
        default:
            return QString();
        }

    case Qt::BackgroundRole:
        return highlight?QBrush(QColor(255,255,0,255)):QBrush();

    default:
        return QTreeWidgetItem::data(column, role);
    }
}

int RemiItem::type() const
{
    return QTreeWidgetItem::UserType;
}

void RemiItem::deleteRemi()
{
    if ( db.isOpen() || db.open() )
    {
        QSqlQuery("DELETE FROM `remission` WHERE `id` = '" + QVariant(this->id).toString() + "';", db);
    }
}

void RemiItem::setHighlighted(bool highlight)
{
    this->highlight = highlight;
}

void RemiItem::setPaket(int paket)
{
    if ( db.isOpen() || db.open() )
    {
        QSqlQuery q(db);
        if ( q.exec("UPDATE `remission` SET `paket_id` = '" + QVariant(paket).toString() + "' WHERE `id` = '" + QVariant(this->id).toString() + "';") )
        {
            this->paket = paket;
        }
        q.finish();
    }
}

void RemiItem::setTitle(QString title)
{
    QSqlQuery q(db);
    if ( ( db.isOpen() || db.open() ) && q.exec("UPDATE `remission` SET `titel` = '" + title + "' WHERE `id` = '" + QVariant(this->id).toString() + "';") )
    {
        this->title = title;
    }
    q.finish();
}

void RemiItem::addQuantity(int add)
{
    this->setQuantity( quantity + add );
}

void RemiItem::setQuantity(int quan)
{
    if ( db.isOpen() || db.open() )
    {
        QSqlQuery q(db);

        if ( quan <= 0 )
        {
            emit deleteItem(this);
        }
        else
        {
            QDateTime currentDateTime = QDateTime::currentDateTime();

            if ( q.exec("UPDATE `remission` SET `menge` = '" + QVariant(quan).toString() + "', `datetime` = '" + currentDateTime.toString("yyyy-MM-dd hh:mm:ss.zzz") + "' WHERE `id` = '" + QVariant(this->id).toString() + "';") )
            {
                this->quantity = quan;
                this->lastedit = currentDateTime;
                emit itemChanged();
            }
        }

        q.finish();
    }
}

QDateTime RemiItem::getLastEdit() const
{
    if ( !lastedit.isNull() )
    {
        return this->lastedit;
    }
    else
    {
        return QDateTime();
    }
}

int RemiItem::getID() const
{
    return id;
}

int RemiItem::getPaket() const
{
    return paket;
}

QString RemiItem::getGTIN() const
{
    return gtin;
}

QString RemiItem::getAddon() const
{
    return addon;
}

int RemiItem::getQuantity() const
{
    return quantity;
}

float RemiItem::getPreis() const
{
    return preis;
}

QString RemiItem::getTitle() const
{
    return title;
}
